<template>
    <Card>
        <Button class="ivu-mb-16" type="primary" @click="handleEdit(false)"
        	v-hasC="{name: '-v1-rbac-role-project-create',flag: '/v1/rbac/role/project-create'}">新增</Button>
        <JG-table
            :params="params"
            :loading="loading"
            :total="total"
            :data="data"
            :columns="columns"
            :butshow="butshow"
            @askData="askData"
            @handleEdit="handleEdit"
            @handleDelete="handleDelete"
            @handleEmpower="handleEmpower"></JG-table>
              <modal ref="modal" @askData="askData"/>
            <Drawer title="权限设置" :closable="true" v-model="drawer"  @askData="askData" width="90%">
                <permissions ref="permissions" @askData="askData"/>
            </Drawer>
    </Card>
</template>
<script>
    import {
        getRoleList,
        deleteRoleList
    } from '@/api/powersetting.js';
    import modal from './modal';
    import permissions from './permissions';
    export default {
        components: { modal, permissions },
        name: '',
        data () {
            return {
                butshow: {
                    Edit: {
                        name: '-v1-rbac-role-project-update',
                        flag: '/v1/rbac/role/project-update'
                    },
                    Delete: {
                        name: '-v1-rbac-role-project-delete',
                        flag: '/v1/rbac/role/project-delete'
                    },
                    Empower: {
                        name: '-v1-rbac-role-project-permission',
                        flag: '/v1/rbac/role/project-permission'
                    }
                },
                loading: false,
                total: -1,
                params: { page: 1, pageSize: 10 },
                paramsinitial: [1, 2, 3],
                columns: [
                    { title: 'ID', key: 'id' },
                    { title: '角色名称', key: 'name' },
                    // { title: '状态', slot: 'status', },
                    { title: '备注', slot: 'type' },
                    { title: '操作', slot: 'powersettingAction', width: 160 }
                ],
                data: [],
                drawer: false
            }
        },
        mounted () {
            this.initial()
        },
        methods: {
            initial () {
                this.askData()
            },
            askData () {
                this.loading = true
                getRoleList(this.params).then(res => {
                    this.data = res
                    this.loading = false
                }).catch(() => {
                    this.loading = false;
                });
            },
            handleEdit (row) {
                this.$refs.modal.initial(row)
            },
            handleDelete (row) {
                this.$Modal.confirm({
                    title: '删除角色',
                    content: `<p>确认删除该角色吗？</p>`,
                    onOk: () => {
                        deleteRoleList({ id: row.id }).then(() => {
                            this.$Message.info('删除成功');
                            this.askData();
                        });
                    }
                });
            },
            handleEmpower (row) {
                this.drawer = true
                this.$refs.permissions.initial(row)
            }
        }
    }
</script>
